package com.example.shop_2302.userInfo

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Cons
import com.example.shop_2302.R
import com.example.shop_2302.databinding.ActivityUpdateBinding

@Route(path = "/app/UpdateActivity")
class UpdateActivity : BaseActivity<ActivityUpdateBinding,UserViewModel>() {
    override fun initView() {
        val phone = SPUtils.getInstance().getString(Cons.PARAM_PHONE)
        val phoneString = phone.replaceRange(3, 7, "****")
        binding.tvPhone.text = "当前手机号：$phoneString"
        binding.btnCode.setOnClickListener {
            viewModel.sendCode(phone)
        }
        binding.btnUpdate.setOnClickListener {
            viewModel.update(phone,binding.etCode.text.toString(),binding.etPwd.text.toString())
        }
    }

    override fun initData() {
        viewModel.sendSuccess.observe(this){
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }
        }
        viewModel.updateSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                finish()
            }
        }
    }

    override fun getViewModel(): Class<UserViewModel>  = UserViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_update

}